const express = require('express');
const router = express.Router();

// 一、服务器代理
const {createProxyMiddleware} = require('http-proxy-middleware');

// 1.堆糖
const dtMiddleware = createProxyMiddleware({
    "target": "https://www.duitang.com", // 目标服务器
    "changeOrigin": true,

    // 路径替换
    "pathRewrite": {
        "^/proxy/dt" : "/"
    }
})
// 2.阿玛尼
const amnMiddleware = createProxyMiddleware({
    "target": "https://www.armani.cn", // 目标服务器
    "changeOrigin": true,

    // 路径替换
    "pathRewrite": {
        "^/proxy/amn" : "/"
    }
})


// 二、jsonp代理

// http://localhost:2003/proxy/jsonp?callback=getCallback

router.get('/jsonp',(req,res)=>{
    const {callback} = req.query;   // 获取callback值-> getCallback

    // 假数据
    const data = {
        good:"honor8",
        price:5669,
        imgurl:'./img/honor8.jpg',
        description:'荣耀8产品的相关介绍'
    }

    res.send(`${callback}(${JSON.stringify(data)})`)
})


router.use('/dt',dtMiddleware);
router.use('/amn',amnMiddleware);

/* 1.堆糖 */
// http://localhost:2003/proxy/dt/napi/blog/list/by_filter_id/?include_fields=top_comments%2Cis_root%2Csource_link%2Citem%2Cbuyable%2Croot_id%2Cstatus%2Clike_count%2Csender%2Calbum%2Creply_count&filter_id=%E5%AE%B6%E5%B1%85%E7%94%9F%E6%B4%BB&start=24&_=1596619679908

/* 2.阿玛尼 */
// http://localhost:2003/proxy/amn/rest/default/V1/cms/page/home

module.exports = router;
